/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.tata;

import edu.fjut.se1603.tata.dao.AccountDao;
import edu.fjut.se1603.tata.util.Log_in;
import edu.fjut.se1603.tata.dao.PetDao;
import edu.fjut.se1603.tata.dao.PetOwnerDao;
import edu.fjut.se1603.tata.dao.PetStoreDao;
import edu.fjut.se1603.tata.dao.impl.AccountDaoOracleImpl;
import edu.fjut.se1603.tata.dao.impl.PetDaoOracleImpl;
import edu.fjut.se1603.tata.dao.impl.PetOwnerDaoOracleImpl;
import edu.fjut.se1603.tata.dao.impl.PetStoreDaoOracleImpl;
import edu.fjut.se1603.tata.entity.Account;
import edu.fjut.se1603.tata.entity.Pet;
import edu.fjut.se1603.tata.entity.PetOwner;
import edu.fjut.se1603.tata.entity.PetStore;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author admin
 */
public class Client {
    public static void main(String  []args)
    {
        System.out.println("宠物商店启动");
       System.out.println("WonderLand醒来，所有宠物从Oracle醒来");
        System.out.println("************************************************************");
        PetDao p=new PetDaoOracleImpl();
        List<Pet> l1=p.getAllPet();
        for(Pet pet:l1)
        {
            System.out.println("第"+pet.getId()+"个宠物，名字叫："+pet.getName());
        }
        System.out.println("************************************************************");
        System.out.println("");
        System.out.println("所有宠物主人从Oracle中醒来");
        System.out.println("************************************************************");
        PetOwnerDao po=new PetOwnerDaoOracleImpl();
        List<PetOwner> l2=po.getAllOwners();
       for(PetOwner petOwner:l2)
        {
            System.out.println("第"+petOwner.getId()+"个宠物主人，名字叫："+petOwner.getName());
        }
        System.out.println("************************************************************");
         System.out.println("");
        System.out.println("所有宠物商店从Oracle中醒来");
        System.out.println("************************************************************");
        PetStoreDao ps=new PetStoreDaoOracleImpl();
        List<PetStore> l3=ps.getAllStores();
        for(PetStore petStore:l3)
        {
            System.out.println("我的名字叫："+petStore.getName());
        }
        System.out.println("************************************************************");
        Log_in iLog_in=new Log_in();
        int k=iLog_in.login();
       Scanner input=new Scanner(System.in); 
        if(k>0)
        { 
            int x=input.nextInt();
            if (x==1) {
                    List<Pet> l4=p.getAllPet();
                  for(Pet pet:l4)
                  {
                       System.out.println("序号："+pet.getId()+"我的名字叫："+pet.getName()+" 我是："+pet.getKind()+"要购买我要花："+pet.getPrice()+"个元宝11");
                   }
                        System.out.println("--------请选择要购买哪一个宠物，并选择选择项的序号----------");
                  int z=input.nextInt();
                AccountDao ad=new AccountDaoOracleImpl();
                  Account a=new Account();
                   List<Account> l5=ad.getAll();
                   int accountid=0;
                  for(Account account:l5)
                  {
                       accountid++;
                    
                   }  
                 a.setDeal_type(String.valueOf(1));
                 a.setPet_id(String.valueOf(z));
                 a.setId(accountid+1);
                  for(Pet pet:l4)
                  {
                      if ( pet.getId()==z) {
                         a.setSeller_id(String.valueOf(pet.getSellerid()));
                         a.setPrice(pet.getPrice());
                      }

                   }
                 a.setBuyer_id(String.valueOf(k));
                String NowtimeString=new  SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
                java.sql.Date mysqlDate=java.sql.Date.valueOf(NowtimeString);
                a.setDeal_time(mysqlDate);
               ad.add(a);
                for (PetOwner petOwner : l2) {
                    if (k==petOwner.getId()) {
                              po.modify(k, (petOwner.getMoney() - a.getPrice()));
                    }
                }
          
                p.delete(z);
                System.out.println("台账正确插入一条信息");
            }
            
        }
        else if (k<0)
        {
            System.out.println("————————————我的宠物列表——————————");
            List<Pet> l4=p.getAllPet();
              for(Pet pet:l4)
              {
                   System.out.println("序号："+pet.getId()+"我的名字叫："+pet.getName()+" 我是："+pet.getKind());
               }
              System.out.println("————————————请选择要出售的宠物序号——————————");
               int n=input.nextInt();
               System.out.println("———————————你要卖出的宠物信息如下——————————");
               for(Pet pet:l4)
              {
                   if (n==pet.getId()) {
                       System.out.println("宠物名字叫："+pet.getName()+"宠物类别是："+pet.getKind());
                  }
               }
               System.out.println("请确认是否卖出：y代表卖出，n代表不卖");
               String m=input.next();
               if(m.equals("y")||m.equals("Y"))
               {
                    System.out.println("下面现有宠物商店，请你选择你要卖给买家序号：");
                    for(PetStore petStore:l3)
                    {
                        System.out.println("序号"+petStore.getId()+"宠物商店名字叫："+petStore.getName());
                    }
               }
               int nn=input.nextInt();
               int pice=0;
               for (Pet pet : l4) {
                if (pet.getId() == n) {
                    pice=pet.getPrice();
                }
               } 
               for (PetOwner petOwner : l2) {
                if (-k == petOwner.getId()) {
                    po.modify(-k, (petOwner.getMoney()+ pice));
                }
            }
             for (PetStore petStore : l3) {
                 if (nn==petStore.getId()) {
                     ps.modify(nn, petStore.getBalance()-pice);
                 }
            }
             
             p.delete(n);
             AccountDao ad = new AccountDaoOracleImpl();
            Account a = new Account();
            a.setBuyer_id(String.valueOf(nn));
            a.setDeal_type(String.valueOf(2));
            String NowtimeString = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
            java.sql.Date mysqlDate = java.sql.Date.valueOf(NowtimeString);
            a.setDeal_time(mysqlDate);
            a.setPet_id(String.valueOf(n));
            a.setPrice(pice);
            a.setSeller_id(String.valueOf(-k));
            ad.add(a);
               System.out.println("台账正确插入一条信息！");
        }
        else
        {
            System.out.println("————登陆错误——————");
        }
    }
}
